package com.shiny.widgetsdk.network.okhttp.request;

import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.Request;

import java.util.Map;

public class CommRequest {

    public static Request createPostRequest(String url, RequestParams params) {
        return createGetRequest(url,params,null);
    }

    /**
     * @param url
     * @param params
     * @param headers
     * @return可以携带请求头的Post请求
     */
    public static Request createGetRequest(String url, RequestParams params, RequestParams headers) {
        FormBody.Builder mFormBuilder = new FormBody.Builder();
        if (params != null) {
            for (Map.Entry<String,String> entry : params.urlParams.entrySet() ) {
                mFormBuilder.add(entry.getKey(),entry.getValue());
            }
        }
        //添加消息头
        Headers.Builder mHeaderBuilder = new Headers.Builder();
        if(headers != null) {
            for (Map.Entry<String,String> entry : headers.urlParams.entrySet() ) {
                mHeaderBuilder.add(entry.getKey(),entry.getValue());
            }
        }
        FormBody formBody = mFormBuilder.build();
        Headers mHeaders1 = mHeaderBuilder.build();
        Request request = new Request.Builder().url(url)
                .post(formBody).headers(mHeaders1).build();
        return request;
    }


}
